#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node
{
    char name[20];
    long number;
    struct node* next;
};

void interface(char op, struct node* begin);
struct node* creat_node(char* name, long number);
void print_node(struct node * begin);
struct node* search_name_node(char* name, struct node* begin);
void add_node(struct node* pnode, struct node* begin);
void delete_node(struct node* pnode, struct node* begin);
void free_list(struct node* begin);

int main()
{
    char input;
    struct node begin = {};
    begin.next = NULL;
    do
    {
        print_node(&begin);
        scanf("%c", &input);
        interface(input, &begin);
        getchar();
    } while (input != 'q');
    free_list(&begin);
    return 0;
}

void interface(char op, struct node* begin)
{
    if(op == 'a')
    {
        char name[20] = {};
        long number = 0;
        scanf("%s", name);
        scanf("%ld", &number);
        struct node* ob = creat_node(name, number);
        add_node(ob, begin);
    }
    else if(op == 'd')
    {
        char name[20] = {};
        scanf("%s", name);
        struct node * ob = search_name_node(name, begin);
        delete_node(ob, begin);
    }
    else if(op == 's')
    {
        char name[20] = {};
        scanf("%s", name);
        struct node * ob = search_name_node(name, begin);
        printf("number:%ld\n", ob->number);
    }
}

struct node* creat_node(char* name, long number)
{
    struct node* ob = malloc(sizeof(struct node));
    strcpy(ob->name, name);
    ob->number = number;
    ob->next = NULL;
    //printf(":%s:%ld\n",ob->name, ob->number);
    return ob;
}

void print_node(struct node * begin)
{
    while (begin->next != NULL)
    {
        begin = begin->next;
        printf("name:%s, %ld\n", begin->name, begin->number);
    }
}

struct node* search_name_node(char* name, struct node* begin)
{
    while (strcmp(name, begin->name))
    {
        begin = begin->next;
    }
    return begin;
}

void add_node(struct node* pnode, struct node* begin)
{
    while (begin->next != NULL)
        begin = begin->next;
    begin->next = pnode;
}

void delete_node(struct node* pnode, struct node* begin)
{
    struct node* node_now = begin,*node_last = NULL;
    while (node_now != pnode)
    {
        node_last = node_now;
        node_now = node_now->next;
    }
    node_last->next = node_now->next;
    if(node_now->next != NULL)free(node_now);
}

void free_list(struct node* begin)
{
    struct node* next;
    next = begin;
    while (next != NULL)
    {
        next = next->next;
        free(next);
    }
}